<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\controller\Admin;

class Index extends Admin {

	public function index() {
		//会员会员信息
		// set_time_limit(0);
		// $clist = model('company')->select();
		// $umap['birthday'] = ['neq', ''];
		// $ulist = model('WxUser')->where($umap)->select();
		// foreach ($clist as $cv => $cvalue) {
		// 	foreach ($ulist as $uk => $vu) {
		// 		if($vu->name && $vu->phone){
		//
		// 			$jg = model('Qiyeuser')->where(['uid'=>$vu->id, 'company_id'=>$cvalue->id, 'username'=>$vu->name, 'phone'=>$vu->phone])->find();
		// 			if(!$jg){
		// 				//添加用户信息
	 	// 			 $dataq['username'] = $vu->name;
	 	// 			 $dataq['phone'] = $vu->phone;
	 	// 			 $dataq['defaults'] = 1;
	 	// 			 $dataq['uid'] = $vu->id;
	 	// 			 $dataq['company_id'] = $cvalue->id;
	 	// 			db('Qiyeuser')->insert($dataq);
	 	// 			 print_r($dataq);
		//
		// 			}
		// 		}
		// 	}
		// }


		if(getGroupId()==6){
			$uid        = session('user_auth.uid');

            $path = 'uploads/QrcPic/';
			//首页
			$url = "http://www.xgw0851.com/index/index/index/rebate_code/".$uid;
			$fileName = $path.$uid.'_index.png';
            $this->createQRcode($url, $fileName);
            //预定
            $yd_url = "http://www.xgw0851.com/index/index/delivery/rebate_code/".$uid;
            $yd_fileName = $path.$uid.'_yd.png';
            $this->createQRcode($yd_url, $yd_fileName);
            //直发
            $zf_url = "http://www.xgw0851.com/index/index/placepinkage/rebate_code/".$uid;
            $zf_fileName = $path.$uid.'_zf.png';
            $this->createQRcode($zf_url, $zf_fileName);

						//直发显示客服
            $zf_url_xs = "http://www.xgw0851.com/index/index/placepinkage/rebate_code/".$uid.'/showcall/1';
            $zf_fileName_xs = $path.$uid.'_zf_xs.png';
            $this->createQRcode($zf_url_xs, $zf_fileName_xs);

						//转盘游戏
						$dzpUrl = "http://www.xgw0851.com/index/game/dzp/rebate_code/".$uid;
						$dzp_fileName = $path.$uid.'_dzp.png';
            $this->createQRcode($dzpUrl, $dzp_fileName);

            $data = [
                'url' => $url,
                'filename' => '/'.$fileName,
                'yd_url' => $yd_url,
                'yd_filename' => '/'.$yd_fileName,
                'zf_url' => $zf_url,
                'zf_filename' => '/'.$zf_fileName,
                'zf_url_xs' => $zf_url_xs,
                'zf_fileName_xs' => '/'.$zf_fileName_xs,
                'dzpUrl' => '/'.$dzpUrl,
                'dzp_fileName' => '/'.$dzp_fileName,
            ];

			$this->assign($data);

		}



		$this->setMeta('后台首页');
		return $this->fetch();
	}


	public function createQRcode($url,$fileName, $level ='L', $size="10"){
        vendor("phpqrcode.phpqrcode");
        if(!file_exists($fileName )){
            \QRcode::png($url, $fileName, $level, $size);
        }
    }


	public function login($username = '', $password = '', $verify = '') {
		if (IS_POST) {
			if (!$username || !$password) {
				return $this->error('用户名或者密码不能为空！', '');
			}

			//验证码验证
			$this->checkVerify($verify);

			$user = model('User');
			$uid  = $user->login($username, $password);
			if ($uid > 0) {
				return $this->success('登录成功！', url('admin/index/index'));
			} else {
				switch ($uid) {
				case -1:$error = '用户不存在或被禁用！';
					break; //系统级别禁用
				case -2:$error = '密码错误！';
					break;
				default:$error = '未知错误！';
					break; // 0-接口参数错误（调试阶段使用）
				}
				return $this->error($error, '');
			}
		} else {
			$param = input('xgw');
			if($param == 100){
				return $this->fetch();
			}

		}
	}

	public function logout() {
		$user = model('User');
		$user->logout();
		$this->redirect('admin/index/login');
	}

	public function clear() {
		if (IS_POST) {
			$clear = input('post.clear/a', array());
			foreach ($clear as $key => $value) {
				if ($value == 'cache') {
					\think\Cache::clear(); // 清空缓存数据
				} elseif ($value == 'log') {
					\think\Log::clear();
				}
			}
			return $this->success("更新成功！", url('admin/index/index'));
		} else {
			$keylist = array(
				array('name' => 'clear', 'title' => '更新缓存', 'type' => 'checkbox', 'help' => '', 'option' => array(
					'cache' => '缓存数据',
					'log'   => '日志数据',
				),
				),
			);
			$data = array(
				'keyList' => $keylist,
			);
			$this->assign($data);
			$this->setMeta("更新缓存");
			return $this->fetch('public/edit');
		}
	}
}
